<?php
/**
 * 权限/菜单 管理控制器
 */

namespace App\Http\Controllers\UserCenter;

use App\Exceptions\ValidatorException;
use App\Http\Controllers\Controller;
use App\Http\Repositories\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class RulesController extends Controller
{
    private $repository;
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 获取
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getRulesList(Request $request)
    {
        //验证规则
        $rules = [
            'operate' => 'required|int|in:1,2',
            'title' => 'nullable|string',
            'Rules_type' => 'nullable|int',
            'create_time' => 'nullable|array'
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getRulesList($validator->getData());
    }

    /**
     * 详情
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getRulesInfo(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required|int'
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getRulesInfo($validator->getData());
    }

    /**
     * 添加
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function addRules(Request $request)
    {
        //验证规则
        $rules = [
            'rule' => 'required|string',
            'module' => 'required|string',
            'controller' => 'required|string',
            'method' => 'nullable|string',
            'path' => 'nullable|string',
            'icon' => 'nullable|string',
            'parent_id' => 'required|int',
            'status' => 'nullable|int',
            'is_show' => 'nullable|int',
            'sort' => 'required|int',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addRules($validator->getData());
    }

    /**
     * 删除
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function delRules(Request $request)
    {
        //验证规则
        $rules = [
            "ids" => "required"
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->delRules($validator->getData());
    }

    /**
     * 编辑
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function editRules(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required',
            'rule' => 'nullable|string',
            'module' => 'nullable|string',
            'controller' => 'nullable|string',
            'method' => 'nullable|string',
            'icon' => 'nullable|string',
            'parent_id' => 'nullable|int',
            'status' => 'nullable|int',
            'is_show' => 'nullable|int',
            'sort' => 'nullable|int',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->editRules($validator->getData());
    }

}
